package com.jinwei.qunguanjia.system.utils;

import com.jinwei.qunguanjia.common.config.QunguanjiaConfig;
import com.jinwei.qunguanjia.system.dto.SysLoginUser;
import com.jinwei.qunguanjia.common.enums.HttpResponseEnum;
import com.jinwei.qunguanjia.common.exception.GlobalExceptionHelper;
import com.jinwei.qunguanjia.common.utils.StringUtils;
import com.jinwei.qunguanjia.common.utils.TenantContextUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 安全服务工具类
 * 
 * @author ruoyi
 */
public class SecurityUtils
{
    /**
     * 用户ID
     **/
    public static String getUserId()
    {
        try
        {
            return getLoginUser().getUserId();
        }
        catch (Exception e)
        {
            throw GlobalExceptionHelper.localServiceException(HttpResponseEnum.UNAUTHORIZED.getCode(), "获取用户ID异常");
        }
    }

    /**
     * 获取部门ID
     **/
    public static String getDeptId()
    {
        try
        {
            return getLoginUser().getDeptId();
        }
        catch (Exception e)
        {
            throw GlobalExceptionHelper.localServiceException(HttpResponseEnum.UNAUTHORIZED.getCode(), "获取部门ID异常");
        }
    }
    
    /**
     * 获取用户账户
     **/
    public static String getUserName()
    {
        try
        {
            return getLoginUser().getUsername();
        }
        catch (Exception e)
        {
            throw GlobalExceptionHelper.localServiceException(HttpResponseEnum.UNAUTHORIZED.getCode(), "获取用户账户异常");
        }
    }

    /**
     * 获取用户
     **/
    public static SysLoginUser getLoginUser()
    {
        try
        {
            if(null == getAuthentication() || getAuthentication().getPrincipal().equals("anonymousUser")){
                return null;
            } else {
                return (SysLoginUser) getAuthentication().getPrincipal();
            }

        }
        catch (Exception e)
        {
            throw GlobalExceptionHelper.localServiceException(HttpResponseEnum.UNAUTHORIZED.getCode(), "获取用户信息异常");
        }
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication()
    {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password)
    {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword 真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    private static boolean matchesPassword(String rawPassword, String encodedPassword)
    {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 是否为管理员
     * @return 结果
     */
    public static boolean isAdmin() {
        return getLoginUser().getUserId() != null && QunguanjiaConfig.getAdminUserId().equals(getLoginUser().getUserId());
    }
    /**
     * 是否为管理员
     * @return 结果
     */
    public static boolean isAdmin(String userId) {
        return StringUtils.isNotEmpty(userId) && QunguanjiaConfig.getAdminUserId().equals(userId);
    }

    /**
     * 角色是否为管理员
     * @return 结果
     */
    public static boolean isRoleAdmin(String roleId) {
        return StringUtils.isNotEmpty(roleId) && QunguanjiaConfig.getAdminRoleId().equals(roleId);
    }
    public static void main(String [] args){
        System.out.println(encryptPassword("admin"));
        System.out.println(encryptPassword("admin123"));
        System.out.println(matchesPassword("admin","$2a$10$720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2"));
        System.out.println(matchesPassword("admin123","$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2"));
    }
}
